<%@ page import="org.biojava.services.das.registry.ConfigBean"%>
<%@ page import="org.springframework.context.ApplicationContext"%>
<%@ page import="org.springframework.context.support.ClassPathXmlApplicationContext"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.sql.*"%>
<%@ page import="org.biojava.services.das2.Das2Mirror"%>
<%@ page import="org.biojava.services.das.registry.DasRegistrySql"%>
<%@ page import="org.biojava.services.das.dao.DasSourceManager" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="org.biojava.dasobert.dasregistry.DasSource"%>
<jsp:directive.page import="org.springframework.beans.factory.BeanFactory"/>
<jsp:directive.page import="org.springframework.context.support.ClassPathXmlApplicationContext"/>
<% String title="mirrored DAS servers";
   String header=title;
%>

<%@ include file="sangerheader.jsp" %>


<div id="main">
<h2>Mirrored DAS servers</h2>

<p>
The DAS registration server can mirror the information provided by DAS servers that
describe all DAS sources with the SOURCES command. This page lists which DAS servers
are currently mirrored like this.
</p>


<%

ConfigBean configBean;
ApplicationContext context;

context = new ClassPathXmlApplicationContext("config_dasregistry.xml");

configBean = (ConfigBean) context.getBean("configBean");
List mirrors = configBean.getMirrorSources();
Iterator iter = mirrors.iterator();

%>

<%@ include file="sangertablestart.jsp" %>

<tr id="row0">
<td></td>
<td>server name </td>
<td> location </td>
<td> URI of server </td>
<td> nr of DAS sources </td>
</tr>
<% 

BeanFactory ctx=(new ClassPathXmlApplicationContext("SpringDAO.xml"));
		DasRegistrySql registry=(DasRegistrySql)ctx.getBean("registryBean");
		
		DasSourceManager dao=(DasSourceManager) ctx.getBean("dasSourceManager");
DataSource datas = registry.getDataSource();
Connection conn = null;
try {
	conn = datas.getConnection();


String selected = request.getParameter("selected");
if ( selected == null )
	selected = "";

int twodays = 1000*60*60*24*2 ;
java.util.Date now = new java.util.Date();

while (iter.hasNext() ) {
	Das2Mirror mirror = (Das2Mirror) iter.next();
	%>
	<tr id="row1">
	<td></td>
	<td><%=mirror.getName() %></td>
	<td><a href="<%=mirror.getUrl() %>"><%=mirror.getUrl() %></a></td>
	<td><%=mirror.getUriPrefix() %></td>
	<td><%
	
	// get the nr of DAS source with this prefix form the registry.
	DasSource[] sources = dao.getCachedSources(conn,mirror.getUriPrefix()+"%");
	%><a href="showMirrored.jsp?selected=<%=mirror.getName() %>"><%
	out.print(sources.length);
	
	%></a></td>
	
	</tr>
			
	<% 
	
	if ( selected.equals(mirror.getName())){
		%>
		<tr id="row2">
		<td colspan="5" align="center">
		<form action="showMirrored.jsp" method="GET">
		<input type="submit" value="close details">
		</form>
	
		</td>
		</tr>
		<%
		for ( int i=0 ; i < sources.length;i++) {
			DasSource ds = sources[i];
		%>
		<tr id="row2">
		
		<td><div title="header=[show details] body=[<%=ds.getDescription() %>]"><a href="showdetails.jsp?auto_id=<%=ds.getId()%>"> 
				<img src="img/messagebox_info16x16.png" border="0" alt="details"
				onMouseOver="window.status='show details'; return true;"
				onMouseOut="window.status=' '; return true;"/>
				</a></div></td>
<td><a href="showdetails.jsp?auto_id=<%=ds.getId()%>"><%=ds.getId() %></a></td>

<td><%=ds.getNickname() %></td>
<td><%=ds.getUrl() %></td>
<td>
<% 
if (ds.getLeaseDate().getTime() < ( now.getTime() - twodays)){
	%><div title="header=[server down?] body=[it was not possible to validate this server in > 2 days]">
	<img src="img/messagebox_warning.png"/>
	</div><% 
}
%>
</td>
</tr>
		
		
		<%	
		}
	}
}


} catch (SQLException e) {
	e.printStackTrace();
} finally {
	if ( conn != null ) {
		try {
			conn.close();
		} catch (SQLException e){
			e.printStackTrace();
		}
	}
}


%>
<%@ include file="sangertableend.jsp" %>

<script src="boxover.js"></script>
<%@ include file="sangerfooter.jsp" %>

